'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async token() {
    const {
      ctx
    } = this;
    let code = ctx.query.code;
    if (code == undefined) {
      ctx.body = "code is null"
    } else {
      let token = await ctx.service.token.getToken();
      const result = await ctx.curl('https://open.welink.huaweicloud.com/api/auth/v2/userid?code='+code, {
        // 必须指定 method
        method: 'GET',
        // 通过 contentType 告诉 HttpClient 以 JSON 格式发送
        contentType: 'json',
        // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
        dataType: 'json',
        headers: {
          'x-wlk-Authorization': token
        }
      });
      let {userId} = result.data;
      if(userId==undefined){
        ctx.body = 'code已失效';
        return;
      }
      console.log('--users---')
      const result1 = await ctx.curl('https://open.welink.huaweicloud.com/api/contact/v1/users?userId='+userId, {
        // 必须指定 method
        method: 'GET',
        // 通过 contentType 告诉 HttpClient 以 JSON 格式发送
        contentType: 'json',
        // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
        dataType: 'json',
        headers: {
          'x-wlk-Authorization': token
        }
      });
      ctx.body = result1.data;
    }

  }
}

module.exports = HomeController;